<?php
class Polls_model extends MY_Model {
	private $module = 'polls';
	private $table = 'polls';

	function getsearchContent($limit,$page){
		$this->db->select('*');
		$this->db->limit($limit,$page);
		$this->db->order_by($this->input->post('func_order_by'),$this->input->post('order_by'));
		if($this->input->post('content')!='' && $this->input->post('content')!='type here...'){
			$this->db->where('(`title_vi` LIKE "%'.$this->input->post('content').'%" OR `title_en` LIKE "%'.$this->input->post('content').'%" OR `title_gr` = "'.$this->input->post('content').'")');
		}
		if($this->input->post('dateFrom')!='' && $this->input->post('dateTo')==''){
			$this->db->where('created >= "'.date('Y-m-d 00:00:00',strtotime($this->input->post('dateFrom'))).'"');
		}
		if($this->input->post('dateFrom')=='' && $this->input->post('dateTo')!=''){
			$this->db->where('created <= "'.date('Y-m-d 23:59:59',strtotime($this->input->post('dateTo'))).'"');
		}
		if($this->input->post('dateFrom')!='' && $this->input->post('dateTo')!=''){
			$this->db->where('created >= "'.date('Y-m-d 00:00:00',strtotime($this->input->post('dateFrom'))).'"');
			$this->db->where('created <= "'.date('Y-m-d 23:59:59',strtotime($this->input->post('dateTo'))).'"');
		}
		$query = $this->db->get(PREFIX.$this->table);

		if($query->result()){
			return $query->result();
		}else{
			return false;
		}
	}

	function getTotalsearchContent(){
		$this->db->select('*');
		if($this->input->post('content')!='' && $this->input->post('content')!='type here...'){
			$this->db->where('(`title_vi` LIKE "%'.$this->input->post('content').'%" OR `title_en` LIKE "%'.$this->input->post('content').'%" OR `title_gr` = "'.$this->input->post('content').'")');
		}
		if($this->input->post('dateFrom')!='' && $this->input->post('dateTo')==''){
			$this->db->where('created >= "'.date('Y-m-d 00:00:00',strtotime($this->input->post('dateFrom'))).'"');
		}
		if($this->input->post('dateFrom')=='' && $this->input->post('dateTo')!=''){
			$this->db->where('created <= "'.date('Y-m-d 23:59:59',strtotime($this->input->post('dateTo'))).'"');
		}
		if($this->input->post('dateFrom')!='' && $this->input->post('dateTo')!=''){
			$this->db->where('created >= "'.date('Y-m-d 00:00:00',strtotime($this->input->post('dateFrom'))).'"');
			$this->db->where('created <= "'.date('Y-m-d 23:59:59',strtotime($this->input->post('dateTo'))).'"');
		}
		$query = $this->db->count_all_results(PREFIX.$this->table);

		if($query > 0){
			return $query;
		}else{
			return false;
		}
	}

	function getDetailManagement($id){
		$this->db->select('*');
		$this->db->where('id',$id);
		$query = $this->db->get(PREFIX.$this->table);

		if($query->result()){
			return $query->result();
		}else{
			return false;
		}
	}

	function saveManagement(){
		if($this->input->post('statusAdmincp')=='on'){
			$status = 1;
		}else{
			$status = 0;
		}

		if($this->input->post('hiddenIdAdmincp')==0){
			//Kiểm tra đã tồn tại chưa
			$data = array(
				'title_vi'=> $this->input->post('titleVnAdmincp'),
				'title_en'=> $this->input->post('titleEnAdmincp'),
				'title_gr'=> $this->input->post('titleGrAdmincp'),
				'status'=> $status,
				'created'=> date('Y-m-d H:i:s',time()),
				'modified' => date('Y-m-d H:i:s',time())
			);
			if($this->db->insert(PREFIX.$this->table,$data)){
				modules::run('admincp/saveLog',$this->module,$this->db->insert_id(),'Add new','Add new');
				return true;
			}
		}else{
			$result = $this->getDetailManagement($this->input->post('hiddenIdAdmincp'));


			$data = array(
				'title_vi'=> $this->input->post('titleVnAdmincp'),
				'title_en'=> $this->input->post('titleEnAdmincp'),
				'title_gr'=> $this->input->post('titleGrAdmincp'),
				'status'=> $status,
				'modified' => date('Y-m-d H:i:s',time())
			);
			modules::run('admincp/saveLog',$this->module,$this->input->post('hiddenIdAdmincp'),'','Update',$result,$data);
			$this->db->where('id',$this->input->post('hiddenIdAdmincp'));
			if($this->db->update(PREFIX.$this->table,$data)){
				return true;
			}
		}
		return false;
	}

	/*----------------------FRONTEND----------------------*/
	function answer_polls($id)
	{
		$count_polls = $this->count_polls_by_id($id);
		$this->update_count_polls($id,$count_polls->count_poll);
	}

	function count_polls_by_id($id)
	{
		$this->db->select('count_poll');
		$this->db->where('id',$id);
		$query = $this->db->get(PREFIX.'polls');
		if($query->num_rows() > 0)
		{
			return $query->row();
		}
		return false;
	}

	function update_count_polls($id,$count_poll)
	{
		$data = array('count_polls'=>$count_poll+1);
		$this->db->where('id',$id);
		if($this->db->update(PREFIX.$this->table,$data))
		{
			return true;
		}
		return false;
	}

	function list_polls()
	{
		$this->db->select('*');
		$this->db->where('status',1);
		$this->db->limit(4);
		$this->db->order_by('order_view','DESC');
		$query = $this->db->get(PREFIX.'polls');
		if($query->num_rows() > 0)
		{
			return $query->result();
		}
		return false;
	}

	function get_poll(){
		$this->db->select('*');
		$this->db->where('status',1);
		$this->db->order_by('order_view','DESC');
		$query = $this->db->get(PREFIX.'polls');
		if($query->num_rows() > 0)
		{
			$result = $query->result();
			return $result[0];
		}
		return false;
	}

	function get_poll_answer($poll_id = 0){
		if($poll_id > 0){
			$this->db->select('*');
			$this->db->where('status',1);
			$this->db->where('poll_id',$poll_id);
			$query = $this->db->get(PREFIX.'polls_answer');
			if($query->num_rows() > 0)
			{
				return $query->result();
			}
			return false;
		}
		return false;
	}

	/*--------------------END FRONTEND--------------------*/
}
